﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BIZ;
using System.Web.Script.Serialization;
using DomainModel;
using System.Collections;
using System.Text.RegularExpressions;

namespace joyAdmin.Controllers
{
    [DomainConfirmationFilter]
    [BIZ.AdminFilter]
    public class systemController : Controller
    {
        //
        // GET: /system/

        RoleOper roleOper = new RoleOper();
        OperatorOper operatorOper = new OperatorOper();
        sectionsOper sectionOper = new sectionsOper();

        public ActionResult changpwd()
        {
            cmsOper.CheckUserRights("changpwd");
            return View();
        }
        #region 操作员
        public ActionResult OperatorManage()
        {
            cmsOper.CheckUserRights("OperatorManage");
            ViewData["roleList"] = roleOper.getRoleList();
            return View();
        }

        [ValidateInput(false)]
        public ActionResult save()
        {
            Operator _staff = new JavaScriptSerializer().Deserialize<Operator>(Request.Form["operator"].ToString());            
            _staff.O_Password = BIZ.StringTool.md5("123456", 16);//新建操作用户密码默认123456            
            if (_staff.O_id != 0)
            {
                _staff.O_lastOid = ((DomainModel.cmsuser)Session["cmsuser"]).id;
                _staff.O_lastTime = DateTime.Now;
                _staff.O_Createtime = _staff.O_Createtime.ToLocalTime();
            }
            else
            {
                _staff.O_Createtime = DateTime.Now;
                _staff.O_addTime = DateTime.Now;
                _staff.O_oid = ((DomainModel.cmsuser)Session["cmsuser"]).id;
            }
            operatorOper.save(_staff);
            return Json(_staff);
        }

        public JsonResult getOperatorbyid()
        {
            string id = Request.Form["res"];
            IList li = operatorOper.getOperatorbyid(int.Parse(id));
            if (li.Count > 0)
            {
                return Json(li[0]);
            }
            else
            {
                return Json("");
            }
        }

        public JsonResult delete()
        {
            string ids = Request.Form["res"];
            return Json(operatorOper.delete(ids));
        }

        public JsonResult getOperatorlist()
        {
            int pageno = Convert.ToInt32(Request.Params["pageno"]);
            int pagesize = Convert.ToInt32(Request.Params["pagesize"]);
            return Json(operatorOper.getOperator(pageno, pagesize));
        }

        public ActionResult checkWorkid()
        {
            string workid = Request.Form["workid"];
            int id = Request.Form["id"] == null ? 0 : int.Parse(Request.Form["id"]);
            bool flag = operatorOper.getCountByWorkid(workid,id);
            if (flag)
                return Json(1);
            else
                return Json(0);

          
        }
        #endregion

        #region 角色

        public ActionResult RoleManage()
        {
            cmsOper.CheckUserRights("RoleManage");
            ViewData["roleList"] = roleOper.getRoleList();
            ViewData["parentlist"] = sectionOper.getParentList();//父栏目
            return View();
        }

        [ValidateInput(false)]
        public ActionResult saveRole()
        {
            Role _staff = new JavaScriptSerializer().Deserialize<Role>(Request.Form["role"].ToString());
            if (_staff.R_id != 0)
            {
                _staff.R_lastOid=((DomainModel.cmsuser)Session["cmsuser"]).id;
                _staff.R_lastTime = DateTime.Now;
                _staff.R_addTime = _staff.R_addTime.ToLocalTime();
            }
            else
            {
                _staff.R_addTime = DateTime.Now;
                _staff.R_oid = ((DomainModel.cmsuser)Session["cmsuser"]).id;
            }
            roleOper.save(_staff);
            return Json(_staff);
        }

        public JsonResult getRolebyid()
        {
            string id = Request.Form["res"];
            IList li =roleOper.getRolebyid(int.Parse(id));
            if (li.Count > 0)
            {
                return Json(li[0]);
            }
            else
            {
                return Json("");
            }
        }

        public JsonResult deleteRole()
        {
            string ids = Request.Form["res"];
            bool f = operatorOper.getCountByRid(int.Parse(ids));//判断该角色下面有没有管理员，有的话不允许删除
             if (f)
                 return Json("n");
             else
             {
                 return Json(roleOper.deleteRole(ids));
             }
        }

        public ActionResult checkRoleName()
        {
            string name = Request.Form["name"];
            int id = Request.Form["id"] == null ? 0 : int.Parse(Request.Form["id"]);
            bool flag = roleOper.getCountByName(name,id);
            if (flag)
                return Json(1);
            else
                return Json(0);
        }


        public JsonResult getRolelist()
        {
            int pageno = Convert.ToInt32(Request.Params["pageno"]);
            int pagesize = Convert.ToInt32(Request.Params["pagesize"]);
            IList list = roleOper.getRoleList(pageno, pagesize);
            for (int j = 1; j < list.Count; j++)
            {
                Hashtable hs = (Hashtable)list[j];

                if (hs["R_SectionCodeList"] != null)
                {
                    string[] array = hs["R_SectionCodeList"].ToString().Split(',');
                    if (array.Length > 0)
                    {
                        string str = string.Empty;
                        for (int i = 0; i < array.Length; i++)
                        {
                            str = str + ((Hashtable)sectionOper.getNameByCode(array[i].ToString())[0])["name"] + ",";
                        }
                        ((Hashtable)list[j])["R_SectionCodeList"] = str;
                    }
                }
            }
            return Json(list);
        }


        public JsonResult changpassword()
        {
            cmsuser u = (cmsuser)Session["cmsuser"];
            int uid = u.id;
            string oldpwd = Request.Form["opwd"];
            string newpwd = Request.Form["npwd"];
            string confirmpwd = Request.Form["cpwd"];
            if (string.IsNullOrEmpty(oldpwd))
                return Json(6);//旧密码不能为空
            if (string.IsNullOrEmpty(newpwd))
                return Json(2);//新密码不能为空
            if (string.IsNullOrEmpty(confirmpwd))
                return Json(3);//确认密码不能为空
            if (newpwd.Trim() != confirmpwd.Trim())
                return Json(4);//两次密码不一致
            if (BIZ.StringTool.md5(oldpwd, 16) != u.pwd)
                return Json(1);//旧密码不正确
            if (!Regex.IsMatch(newpwd, @"^(\w){4,20}$"))
                return Json(5);//不符合密码验证规则

            operatorOper.updatepwd(BIZ.StringTool.md5(newpwd, 16), uid);
            u.pwd = BIZ.StringTool.md5(newpwd, 16);
            Session["cmsuser"] = u;
            return Json(0);
        }
        #endregion
    }
}
